_DataGrid_FILTER = Class.create({
  initialize : function(column) {
    this._column = column; 
  },
  execute: function (event) {
  },
  getColumn: function() {
    return this._column;
  }
});

_DataGrid_DATAGRIDSTRINGFILTER = Class.create(_DataGrid_FILTER, {
  initialize : function($super, column, filterValue, rowFilterableFunc, inclusive) {
    $super(column);
    this._filterValue = filterValue;
    this._rowFilterableFunc = rowFilterableFunc;
    this._inclusive = inclusive;
  },
  execute: function ($super, event) {
    var dataGrid = event.dataGridComponent;
    var filterFunc = this._rowFilterableFunc;
    var hasFilterFunc = !(this._rowFilterableFunc == undefined);
    
    var hideArray = new Array();
    
    for (var i = dataGrid.getRowCount() - 1; i >= 0; i--) {
      var rowValue = dataGrid.getCellValue(i, this.getColumn());
      var match = this._inclusive ? rowValue != this._filterValue : rowValue == this._filterValue; 

      if (match) {
        if ((!hasFilterFunc) || (filterFunc(dataGrid, i) === true))
          hideArray.push(i);
      }
    }

    if (hideArray.size() > 0)
      dataGrid.hideRows(hideArray.toArray());
  }  
});

DataGrid_ROWSTRINGFILTER = Class.create({
  initialize : function(rowFilterableFunc, inclusive) {
    this._rowFilterableFunc = rowFilterableFunc;
    if (inclusive != undefined)
      this._inclusive = inclusive === false ? false : true;
    else
      this._inclusive = true;
  },
  execute: function (event) {
    var dataGrid = event.dataGridComponent;
    var row = event.row;
    var column = event.column;
    var filterValue = dataGrid.getCellValue(row, column);
    var filter = new _DataGrid_DATAGRIDSTRINGFILTER(column, filterValue, this._rowFilterableFunc, this._inclusive);

    dataGrid.addFilter(filter);
  }
});

_DataGrid_DATAGRIDDATEFILTER = Class.create(_DataGrid_FILTER, {
  initialize : function($super, column, filterValue, filterEarlier, rowFilterableFunc) {
    $super(column);
    this._filterValue = filterValue;
    this._filterEarlier = filterEarlier;
    this._rowFilterableFunc = rowFilterableFunc;
  },
  execute: function ($super, event) {
    var dataGrid = event.dataGridComponent;
    var filterFunc = this._rowFilterableFunc;
    var hasFilterFunc = !((filterFunc == undefined) || (filterFunc == null));
    
    var hideArray = new Array();

    if (this._filterEarlier) {
      for (var i = dataGrid.getRowCount() - 1; i >= 0; i--) {
        var rowValue = dataGrid.getCellValue(i, this.getColumn());
        if ((rowValue) && (rowValue > this._filterValue)) { 
          if ((!hasFilterFunc) || (filterFunc(dataGrid, i) === true))
            hideArray.push(i);
        }
      }
    } else {
      for (var i = dataGrid.getRowCount() - 1; i >= 0; i--) {
        var rowValue = dataGrid.getCellValue(i, this.getColumn());
        if ((rowValue) && (rowValue < this._filterValue)) { 
          if ((!hasFilterFunc) || (filterFunc(dataGrid, i) === true))
            hideArray.push(i);
        }
      }
    }
  
    if (hideArray.size() > 0)
      dataGrid.hideRows(hideArray.toArray());
  }
});

DataGrid_ROWDATEFILTER = Class.create({
  initialize : function(filterEarlier, rowFilterableFunc) {
    this._filterEarlier = filterEarlier;
    this._rowFilterableFunc = rowFilterableFunc;
  },
  execute: function (event) {
    var dataGrid = event.dataGridComponent;
    var row = event.row;
    var column = event.column;
    var filterValue = dataGrid.getCellValue(row, column);

    var filter = new _DataGrid_DATAGRIDDATEFILTER(column, filterValue, this._filterEarlier, this._rowFilterableFunc);
    dataGrid.addFilter(filter);
  }
});

DataGrid_ROWCLEARFILTER = Class.create({
  initialize : function() {
  },
  execute: function (event) {
    var dataGrid = event.dataGridComponent;
    dataGrid.clearFilters();
  }  
});